package com.gscitysfy.cus.drain.modules.duty.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;

import com.gscitysfy.cus.drain.annotations.DictTranslation;
import com.gscitysfy.cus.drain.annotations.FieldParam;
import com.gscitysfy.cus.drain.constants.DicConstant;
import com.gscitysfy.cus.drain.modules.duty.entity.MoperDutyMnChange;
import com.gscitysfy.cus.drain.modules.duty.entity.MoperDutyMnClasses;
import com.gscitysfy.cus.drain.modules.duty.service.IMoperDutyMnChangeService;
import com.gscitysfy.cus.drain.modules.duty.service.IMoperDutyMnClassesService;
import com.gscitysfy.cus.drain.modules.duty.vo.MoperDutyMnChangeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @author 葛守跃
 * @version 1.0.0
 * @ClassName MoperDutyMnChangeController
 * @Description 值班人员交接班记录 前端控制器
 * @creatTime 2022-03-29 16:47:17
 */
@RestController
@RequestMapping("/duty/change")
@Slf4j
@Tag(name = "值班管理->交接班记录相关接口")
public class MoperDutyMnChangeController {

    @Resource
    private IMoperDutyMnChangeService changeService;
    @Resource
    private IMoperDutyMnClassesService classService;

    /**
     * @desc 新增或者修改交班记录
     * @param change
     * @return
     */
    @PostMapping("/saveOrUpdateChangeRecord")
    @Operation(summary = "保存或者修改交接班记录")
    public Result<Boolean> saveOrUpdateChangeRecord(@RequestBody MoperDutyMnChange change){
        //交接班保存值班类别
        MoperDutyMnClasses classInfo = classService.getById(change.getCoamDutyClassId());
        if (ObjectUtil.isNotEmpty(classInfo)) {
            change.setCoamDutyType(classInfo.getCoamClassessType());
        }
        return Result.content(changeService.saveOrUpdate(change));
    }

    /**
     * @desc 删除交接班记录
     * @return
     */
    @PostMapping("/delRecord")
    @Operation(summary = "删除交接班记录", description = "支持多记录删除，id使用逗号分割")
    public Result<Boolean> delRecord(String ids){
        if(StringUtils.isEmpty(ids)){
            return Result.content(false);
        }
        return Result.content(changeService.removeByIds(Arrays.asList(ids.split(StrUtil.COMMA))));
    }

    /**
     * 交接班记录归档，归档的数据不能删除或者修改
     * @param ids
     * @return
     */
    @PostMapping("/dataFile")
    @Operation(summary = "记录归档，已经归档的记录不能修改或者删除", description = "支持多记录归档，id使用逗号分割")
    public Result<Boolean> dataFile(String ids){
        if(StringUtils.isEmpty(ids)){
            return Result.content(false);
        }
        LambdaUpdateWrapper<MoperDutyMnChange> update = new LambdaUpdateWrapper<>();
        update.in(MoperDutyMnChange::getCoamKid, Arrays.asList(ids.split(StrUtil.COMMA))).set(MoperDutyMnChange::getCoamDutyFile, 1);
        return Result.content(this.changeService.update(update));
    }

    /**
     * @desc 分页获取交接班记录
     * @param page
     * @param changeVo
     * @return
     */
    @GetMapping("/getChangeRecord")
    @Operation(summary = "获取归档或未归档的交接班记录", description = "支持多条件查询")
    @DictTranslation({@FieldParam(dictType = DicConstant.DUTY_TYPE, targetField = "coamDutyType")})
    public Result<IPage<MoperDutyMnChange>> getChangeRecord(Page<MoperDutyMnChange> page, MoperDutyMnChangeVo changeVo){
        IPage<MoperDutyMnChange> pages = this.changeService.getChangeRecordByPage(page, changeVo);
        return Result.content(pages);
    }

    /**
     * @desc 导出已归档交接班记录
     * @param changeVo
     * @param response
     */
    @GetMapping("/exportChangeRecord")
    @Operation(summary = "导出归档记录", description = "多条件查询导出结果集")
    public void exportChangeRecord(MoperDutyMnChangeVo changeVo, HttpServletResponse response){
        this.changeService.exportChangeRecord(changeVo, response);
    }


}
